import { _decorator, Component, Node, Label, UITransform } from 'cc';
import { UIManagerDefines } from '../../framework/ui_manager_define';
import { UIBase } from './ui_base';
const { ccclass, property } = _decorator;

/**
 * dialog基础类型
 */
@ccclass('UIDialogBase')
export class UIDialogBase extends UIBase {
    @property(Node)
    btnConfirm: Node | null = null;
    @property(Node)
    btnCancel: Node | null = null;

    @property(Label)
    contentLabel: Label | null = null;

    @property(Node)
    closeBtn: Node | null = null;

    @property(Label)
    titleLabel: Label | null = null;

    _choose: boolean = false;
    private _confirmCall: Function | undefined = undefined;
    private _cancelCall: Function | undefined = undefined;

    onEnable () {
        this.btnConfirm?.on(Node.EventType.TOUCH_END, this.onConfirm, this);
        this.btnCancel?.on(Node.EventType.TOUCH_END, this.onCancel, this);
        this.closeBtn?.on(Node.EventType.TOUCH_END, this.onCancel, this);
    }

    protected _setDefaultBtn () {
        if (this.btnConfirm) {
            this.btnConfirm.active = true;
            this.btnConfirm.setPosition(0, this.btnConfirm.position.y, 0);
            if (this.btnCancel) {
                this.btnCancel.active = false;
            }
        } else if (this.btnCancel) {
            this.btnCancel.active = true;
            this.btnCancel.setPosition(0, this.btnCancel.position.y, 0);
        }
    }

    show (content: string | UIManagerDefines.DialogOptions, ...args: any) {
        if (typeof content == "string") {
            this.contentLabel!.string = content;
            this._setDefaultBtn();
        } else {
            let opt = content as UIManagerDefines.DialogOptions;
            if (opt.title) {
                this.titleLabel!.string = opt.title;
            }
            this._confirmCall = opt.confirmCall;
            this._cancelCall = opt.cancelCall;

            let activeFlag = 0;
            if (this._confirmCall) {
                activeFlag = activeFlag | 1;
            }

            if (this._cancelCall) {
                activeFlag = activeFlag | 2;
            }

            if (activeFlag == 0) {
                this._setDefaultBtn();
            } else {
                this.btnConfirm && (this.btnConfirm.active = (activeFlag & 1) > 0);
                this.btnCancel && (this.btnCancel.active = (activeFlag & 2) > 0);

                if (activeFlag == 3) {
                    this.btnConfirm!.setPosition(- this.btnConfirm!.parent!.getComponent(UITransform)!.width / 4, this.btnConfirm!.position.y, 0);
                    this.btnCancel!.setPosition(this.btnCancel!.parent!.getComponent(UITransform)!.width / 4, this.btnCancel!.position.y, 0);
                } else {
                    let btn;
                    if (activeFlag & 1) {
                        btn = this.btnConfirm;
                    } else {
                        btn = this.btnCancel;
                    }
                    btn!.setPosition(0, btn!.position.y, 0);
                }
            }
        }
    }

    onConfirm () {
        this._choose = true;
        this._confirmCall && this._confirmCall();
        this.onClose();
    }

    onCancel () {
        this._choose = false;
        this._cancelCall && this._cancelCall();
        this.onClose();
    }

    onDisable () {
        this.btnConfirm?.off(Node.EventType.TOUCH_END, this.onConfirm, this);
        this.btnCancel?.off(Node.EventType.TOUCH_END, this.onCancel, this);
        this.closeBtn?.off(Node.EventType.TOUCH_END, this.onCancel, this);

        if (this._disableCall) {
            this._disableCall(this._choose);
        }
    }

}

